new Vue({
  el: "#app",
  data() {
    return {
      // business_article_pic: [],
      upLoadFilesList: new Array(new Array()),
      fmfileList: [],
      djfileList: [],
      VarietyName: "",
      form: [{}],
      a: [],
      form2: [
        {
          GradeName: "",
          // VarietyName: "",
          // Children: [],
        },
      ],

      rules: {
        VarietyName: [
          // { required: true, message: "请输入食用菌品种", trigger: "blur" },
        ],
      },
      loading: false,
    };
  },
  mounted() {},
  computed: {},
  methods: {
    delet_form21(index) {
      console.log(index);
      this.form2.splice(index, 1);
    },
    dhandleRemove(file, index) {
      console.log(index);

      this.upLoadFilesList[index] = new Array();
      this.form2[index].Show_image = "";
      this.$set(this, "upLoadFilesList", [...this.upLoadFilesList]);
    },
    phandleRemove(file, fileList) {
      console.log(file, fileList);
      console.log("Upload change event:", file, fileList);
      const arr = fileList?.filter((i) => i.uid !== file.uid);
      console.log(arr);
      this.$set(this, "fmfileList", arr);
    },
    showFileList: function (index) {
      // return function (url) {
      //   return url ? [url] : [];
      // };
      console.log("index", index, this.upLoadFilesList[index]);

      return this.upLoadFilesList[index];
    },
    dhandleChange(file, fileList) {
      // 这个方法现在主要用于调试或处理其他上传后的逻辑
      // 注意：这里的fileList参数与:file-list绑定的fileList不是同一个对象
      // :file-list绑定的fileList是组件内部维护的，而这里的fileList是上传事件提供的
      console.log("Upload change event:", file, fileList);
    },
    dhandleBeforeUpload(file, index) {
      // console.log(file, fileList);
      // if (index == undefined) return;
      // 生成预览URL并添加到fileList中
      const reader = new FileReader();
      reader.onload = (e) => {
        const newFile = {
          name: file.name,
          url: e.target.result, // 预览URL
          uid: file.uid,
          // 可以添加其他属性，如上传状态等
        };

        this.upLoadFilesList[index].push(newFile);
        console.log(111, this.upLoadFilesList, this.upLoadFilesList[index]);

        // console.log(newFile);
        const _this = this;
        $.ajax({
          // headers: requestHeaders(), // 确保这个函数返回正确的头部信息
          url: "https://service.kdddy.cn/donghuiForRequest/store/photo",
          // url: "http://192.168.10.79:8087/store/photo",
          // url: "http://192.168.10.79:8087/store/photo",
          type: "POST", // 注意HTTP方法应该大写
          contentType: "application/json", // 设置请求体的媒体类型为application/json
          data: JSON.stringify(this.upLoadFilesList[index]), // 将数据序列化为JSON字符串
          success: function (Result) {
            console.log(Result);
            console.log(_this.upLoadFilesList[index]);

            let base64Image = _this.upLoadFilesList[index][0].url;
            let dataIndex = base64Image.indexOf("data:");
            let base64Index = base64Image.indexOf(";base64,");
            let substring = base64Image.substring(dataIndex + 5, base64Index);
            let ls_split = substring.split("/");
            _this.form2[index].Show_image =
              _this.upLoadFilesList[index][0].uid + "." + ls_split[1];
            // upLoadFilesList.push(new Array());
            // let lJSONObject_business_article_pic = new Object();
            // lJSONObject_business_article_pic.business_article_pic =
            //   _this.upLoadFilesList[index];

            // _this.form2[index] = _this.upLoadFilesList[index];
            // _this.upLoadFilesList[index];
            console.log(111, _this.form2[index]);
          },
          complete: function (XMLHttpRequest) {
            requestPretreatment(XMLHttpRequest); // 确保这个函数在config/config.js中定义
          },
          dataType: "json", // 预期服务器返回的数据类型
        });
      };
      reader.readAsDataURL(file);
      // console.log(this.fmfileList);

      // 返回false以阻止自动上传
      return false;
    },
    chandleChange(file, fileList) {
      // 这个方法现在主要用于调试或处理其他上传后的逻辑
      // 注意：这里的fileList参数与:file-list绑定的fileList不是同一个对象
      // :file-list绑定的fileList是组件内部维护的，而这里的fileList是上传事件提供的
      console.log("Upload change event:", file, fileList);
    },
    handleBeforeUpload(file) {
      const allowedExtensions = [".jpg", ".jpeg", ".png", ".gif"];
      const fileExtension = "." + file.name.split(".").pop().toLowerCase();
      if (!allowedExtensions.includes(fileExtension)) {
        this.$message.error("仅支持上传 JPG、JPEG、PNG 和 GIF 图片");
        return;
      }

      // 生成预览URL并添加到fileList中
      const reader = new FileReader();
      reader.onload = (e) => {
        const newFile = {
          name: file.name,
          url: e.target.result, // 预览URL
          uid: file.uid,
          // 可以添加其他属性，如上传状态等
        };
        this.fmfileList.push(newFile);
        $.ajax({
          // headers: requestHeaders(), // 确保这个函数返回正确的头部信息
          url: "https://service.kdddy.cn/donghuiForRequest/store/photo",
          // url: "http://192.168.10.79:8087/store/photo",
          // url: "http://192.168.10.79:8087/store/photo",
          type: "POST", // 注意HTTP方法应该大写
          contentType: "application/json", // 设置请求体的媒体类型为application/json
          data: JSON.stringify(this.fmfileList), // 将数据序列化为JSON字符串
          success: function (Result) {
            console.log(Result);
          },
          complete: function (XMLHttpRequest) {
            requestPretreatment(XMLHttpRequest); // 确保这个函数在config/config.js中定义
          },
          dataType: "json", // 预期服务器返回的数据类型
        });
      };
      reader.readAsDataURL(file);
      // console.log(this.fmfileList);

      // 返回false以阻止自动上传
      return false;
    },
    add_form2() {
      //#Add_Button
      this.form2.push({
        // VarietyName: "",
        GradeName: "",
        // Children: {
        // business_article_group_detail: [
        //   {
        //     id: createUniqueString(),
        //     Specification_Group_Detail_Name: "",
        //   },
        // ],
        // },
      });
      this.upLoadFilesList.push(new Array());
      // this.
      // this.upLoadFilesList[index];
      // this.business_article_group.push({
      //   id: createUniqueString(),
      //   Specification_Group_Detail_Name: "",
      // });

      // this.$forceUpdate();
      // this.form2.Children = this.business_article_group;
      console.log(this.form2);
      console.log(this.upLoadFilesList);
    },
    delet_form2() {
      if (this.form2.length > 1) {
        this.form2.pop();
      } else {
        this.$message({
          message: "不能在删除了",
          type: "warning",
        });
      }
    },
    submitForm(formName) {
      console.log(formName, "456");
      console.log(this.fmfileList);
      if (this.fmfileList.length == 0) {
        this.$message.error("请您上传品种图片");
      } else {
        let base64Image = this.fmfileList[0].url;
        let dataIndex = base64Image.indexOf("data:");
        let base64Index = base64Image.indexOf(";base64,");
        let substring = base64Image.substring(dataIndex + 5, base64Index);
        let ls_split = substring.split("/");
        this.form[0].Show_image = this.fmfileList[0].uid + "." + ls_split[1];
        // console.log(this.form2);
        // console.log(this.form[0].Category_Name, "11111");
        this.form[0].VarietyName = this.VarietyName;

        // console.log(formName);
        // let djlArray = new Array();
        // djlArray.push();

        let lJSONObject_base_EdibleMushroomGrade = new Object();
        lJSONObject_base_EdibleMushroomGrade.base_EdibleMushroomGrade =
          this.form2; //换表

        this.form[0].Children = lJSONObject_base_EdibleMushroomGrade;

        console.log(this.form, "543211");

        const that = this;

        // return;
        this.$refs[formName].validate((valid) => {
          console.log(valid, "contract");

          if (valid) {
            console.log(11111111);

            that.loading = true;
            console.log(valid, "valid");
            console.log(that.loading, "that.loading");
            let lJSONObject_base_EdibleMushroomVariety = new Object();
            lJSONObject_base_EdibleMushroomVariety.base_EdibleMushroomVariety =
              this.form;
            console.log(lJSONObject_base_EdibleMushroomVariety, "11111");
            let lJSONObject_DataSet = new Object(); //Modify
            lJSONObject_DataSet.Add = lJSONObject_base_EdibleMushroomVariety; //换表

            // let lJSONObject_base_EdibleMushroomGrade = new Object();
            // lJSONObject_base_EdibleMushroomGrade.base_EdibleMushroomGrade =
            //   this.form2;

            // let lJSONObject_Children = new Object();
            // this.form[0].Children = lJSONObject_base_EdibleMushroomGrade;

            console.log(lJSONObject_DataSet);

            // let lArray_1 = new Array();
            // lArray_1.push();
            // return;
            $.ajax({
              headers: requestHeaders(),
              // url: `${BASE_URL2}/generalBusiness/generalSave`,
              // url: "https://service.kdddy.cn/donghuiForRequest/add",
              url: "https://service.kdddy.cn/donghui/clientOperation/generalSaveForClient",
              type: "POST",
              data: {
                Data: JSON.stringify(lJSONObject_DataSet),
              },
              success: function (res) {
                console.log(res, "res");
                if (res.Status === "success") {
                  that.$message.success("提交成功");
                  window.history.go(-1);
                } else {
                  that.$message.error("提交失败，请稍后再试");
                }
              },
              complete: function (XMLHttpRequest) {
                that.loading = false;
                // requestPretreatment(XMLHttpRequest); //config/config.js 响应预检
              },
              dataType: "json",
            });
          } else {
            that.$message({
              message: sumberError,
              type: "warning",
            });
            return false;
          }
        });
      }
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
});
